//
//  LXTableViewCell.h
//  ZuLinProject
//
//  Created by bancheng on 2021/12/20.
//  Copyright © 2021 LX_App. All rights reserved.
//
#import "LXTableViewCell.h"

@interface LXTableViewCell ()


@end

@implementation LXTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = White_Color;
        
        [self setClipsToBounds:YES];
        self.titleLab = [[UILabel alloc] init];
        self.titleLab.font = [UIFont systemFontOfSize:16];
        self.titleLab.numberOfLines = 2;
        self.titleLab.textColor = Color333;
        [self.contentView addSubview:self.titleLab];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView.mas_top);
            make.left.equalTo(@15);
            make.width.equalTo(@150);
            make.height.equalTo(self.contentView.mas_height);
        }];
        
        
        self.subTitleLab = [[UILabel alloc] init];
        self.subTitleLab.numberOfLines = 0;
        self.subTitleLab.hidden = YES;
        self.subTitleLab.font = [UIFont systemFontOfSize:15];
        self.subTitleLab.textAlignment = NSTextAlignmentRight;
        self.subTitleLab.textColor = UIColor.blackColor;
        [self.contentView addSubview:self.subTitleLab];
        [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLab.mas_right).offset(0);
            make.top.equalTo(self.contentView.mas_top);
            make.right.equalTo(self.contentView.mas_right).offset(-25);
            make.bottom.equalTo(self.contentView.mas_bottom);
        }];
        
        
        self.textFiled = [[UITextField alloc] init];
        self.textFiled.font=[UIFont systemFontOfSize:15];
        self.textFiled.textColor=Color333;
        [self.contentView addSubview:self.textFiled];
        self.textFiled.returnKeyType = UIReturnKeyDone;
        self.textFiled.delegate=self;
        self.textFiled.textAlignment = 2;
        self.textFiled.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        [self.textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLab.mas_right).offset(0);
            make.top.equalTo(self.contentView.mas_top);
            make.right.equalTo(self.contentView.mas_right).offset(-30);
            make.bottom.equalTo(self.contentView.mas_bottom);
        }];
        
        [self.textFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        
        self.swicth = [[UISwitch alloc] init];
        [self.swicth  setOnTintColor:mainColor];
        [self.swicth setOn:NO];
        self.swicth.hidden = YES;
        [self.contentView addSubview:self.swicth];
        [self.swicth mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView.mas_right).offset(-20);
            make.centerY.equalTo(self.contentView);
        }];
        [self.swicth addTarget:self action:@selector(switchDidChange:) forControlEvents:UIControlEventValueChanged];
        
        self.iconV = [[UIImageView alloc]init];
        self.iconV.image = [UIImage imageNamed:@"下一步"];
        [self.contentView addSubview:self.iconV];
        [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView.mas_right).offset(-5);
            make.width.height.equalTo(@17);
            make.centerY.equalTo(self.contentView);
        }];
        
        self.unit = [[UILabel alloc] init];
        self.unit.hidden = YES;
        self.unit.font = Fond12
        self.unit.textAlignment = NSTextAlignmentCenter;
        self.unit.textColor = Color333;
        self.unit.numberOfLines = 2;
        self.unit.adjustsFontSizeToFitWidth = YES;
        [self.contentView addSubview:self.unit];
        [self.unit mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView.mas_right).offset(0);
            make.left.equalTo(self.textFiled.mas_right).offset(0);
            make.top.equalTo(self.contentView.mas_top).offset(0);
            make.bottom.equalTo(self.textFiled.mas_bottom).offset(0);
//            make.height.equalTo(@20);
//            make.centerY.equalTo(self.contentView);
        }];
        
        
        self.lineV = [[UIView alloc]init];
        self.lineV.backgroundColor = ColorWithF5F5F5;
        [self.contentView addSubview:self.lineV];
        [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left);
            make.right.equalTo(self.contentView.mas_right);
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-0.5);
            make.height.equalTo(@0.5);
        }];
        
    }
    return self;
}

-(void)setCellDic:(NSDictionary *)cellDic{
    _cellDic = cellDic;
  
    if ([cellDic[@"keyboardType"] intValue]!=0) {
        self.textFiled.keyboardType = [cellDic[@"keyboardType"] intValue];
    }
    else{
        self.textFiled.keyboardType = UIKeyboardTypeDefault;
    }
    
    self.titleLab.text = cellDic[@"title"];
    if (!ObjectIsEmpty(cellDic[@"textColor"])) {
        self.titleLab.textColor = cellDic[@"textColor"];
    }
    else {
        self.titleLab.textColor = Color333;
    }
    
    if (!ObjectIsEmpty(cellDic[@"detailTextColor"])) {
        self.textFiled.textColor = cellDic[@"detailTextColor"];
        self.subTitleLab.textColor = cellDic[@"detailTextColor"];
    }
    else {
        self.textFiled.textColor = Color333;
        self.subTitleLab.textColor = Color333;
    }
    
    if (!StringIsEmpty(self.titleLab.text)) {
        [self.titleLab addAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forString:@"*"];
    }
    
    self.textFiled.placeholder = cellDic[@"placeHolder"];
    if (!StringIsEmpty(cellDic[@"placeHolder"])) {
        NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:self.textFiled.placeholder attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}];
        self.textFiled.attributedPlaceholder = placeholderString;
    }
    
    
    if([cellDic[@"canEdit"] boolValue]){
        self.textFiled.userInteractionEnabled = YES;
    }else{
        self.textFiled.userInteractionEnabled = NO;
    }
    
    if ([cellDic[@"showAccess"] boolValue]) {
        self.iconV.hidden = NO;
    }else{
        self.iconV.hidden = YES;
    }
    if (!StringIsEmpty(cellDic[@"showUnit"])) {
        self.unit.hidden = NO;
        self.unit.text = cellDic[@"showUnit"];
    }else{
        self.unit.hidden = YES;
    }
   
    
    
}

- (void) textFieldDidChange:(UITextField *) textField{
   
    NSInteger maxNum= [self.cellDic[@"maxNum"] integerValue];
    if(maxNum != 0 && ([self.cellDic[@"keyboardType"] integerValue] == UIKeyboardTypeDecimalPad ||
       [self.cellDic[@"keyboardType"] integerValue] == UIKeyboardTypeNumberPad)){
     
        NSInteger maxNum= [self.cellDic[@"maxNum"] integerValue];
        if ([textField.text integerValue] > maxNum) {
            textField.text = [NSString stringWithFormat:@"%@",@(maxNum)];
            NSString *msg = [NSString stringWithFormat:@"最多可输入%@",@(maxNum)];
            showAlert(msg);
        }
    }
    
    NSString *textString=textField.text;
    
    if(_textfiledChangeBlock)
        _textfiledChangeBlock(textString);
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSString *textString=textField.text;
    
    if ([textString hasSuffix:@"."]) {
        textField.text = [textField.text substringToIndex:textField.text.length-1];
        if(_textfiledChangeBlock)
            _textfiledChangeBlock(textField.text);
        return;
    }
    else {
        if(self.textfiledDidChangeBlock)self.textfiledDidChangeBlock(textString);
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSString *shouldText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    NSInteger maxLength= [self.cellDic[@"maxLength"] integerValue];
    
    NSInteger strLength = shouldText.length;
    
    if (strLength > maxLength) {
        // 允许删除
        if (strLength < textField.text.length) {
            return YES;
        }
        
        return NO;
    }
    
    //  字母加数字
    if ([self.cellDic[@"inputType"] intValue]==NUM_LATTER) {
        return [NSString numberAndLetter:shouldText];
    }
    // 纯数字
    if ([self.cellDic[@"inputType"] intValue]==NUM) {
        return [NSString numberOnly:shouldText];
    }
    // 纯中文
    if([self.cellDic[@"inputType"] intValue] == CHINESE){
        [ChineseNotificationHelp ChineseNotificationHelpShareWithTextFiled:textField];
    }
    // 纯数字小数点
    if ([self.cellDic[@"inputType"] intValue] == NUM_DECIMAL) {
        return [NSString validateNumberNegativePoint:shouldText];
    }
    
    
    // 1 字母加数字
    if ([self.cellDic[@"inputType"] intValue]==NUM) {
        return [NSString numberAndLetter:shouldText];
    }
 
    if ([self.cellDic[@"keyboardType"] intValue]==UIKeyboardTypeDecimalPad) {
        return [NSString validateNumberNegativePoint:shouldText];
    }
    else if ([self.cellDic[@"keyboardType"] intValue]==UIKeyboardTypeNumberPad){
        return [NSString numberOnly:shouldText];
    }
    
  
    return YES;
    

}



- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(void)switchDidChange:(UISwitch*)sender{
    
    if (self.switchDidChangeBlock) {
        self.switchDidChangeBlock(sender.on);
    }
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
}


@end
